package com.xinyi.basic.app;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * @author 新一
 * @date 2023/3/7 8:59
 * App版本
 */
public class AppConfig {

    /**
     * 返回版本名字
     * 对应build.gradle中的versionName
     */
    public static String getVersionName() {
        String versionName = "";
        try {
            PackageManager packageManager = AppContext.mMainContext.getPackageManager();
            PackageInfo packInfo = packageManager.getPackageInfo(AppContext.mMainContext.getPackageName(), 0);
            versionName = packInfo.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionName;
    }

    /**
     * 返回版本号
     * 对应build.gradle中的versionCode
     */
    public static String getVersionCode() {
        String versionCode = "";
        try {
            PackageManager packageManager = AppContext.mMainContext.getPackageManager();
            PackageInfo packInfo = packageManager.getPackageInfo(AppContext.mMainContext.getPackageName(), 0);
            versionCode = String.valueOf(packInfo.versionCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * 获取AndroidManifest.xml里 的值
     */
    public static String getMetaData(String name) {
        String value = null;
        try {
            ApplicationInfo appInfo = AppContext.mMainContext.getPackageManager().getApplicationInfo(
                    AppContext.mMainContext.getPackageName(),
                    PackageManager.GET_META_DATA
            );
            value = appInfo.metaData.getString(name);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return value;
    }

    /**
     * 获取当前App进程的id
     */
    public static int getAppProcessId() {
        return android.os.Process.myPid();
    }
}
